package com.livegoods.service;

import com.livegoods.commons.pojo.LivegoodsResult;
import com.livegoods.pojo.Comment;
import com.livegoods.pojo.House;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;


    public LivegoodsResult findByHouseId(String houseId,Integer page,Integer size) {

        try {
            Criteria criteria = Criteria.where("houseId").is(houseId);

            Query query = new Query(criteria);

            query.with(PageRequest.of(page,size));

            List<Comment> comments = mongoTemplate.find(query, Comment.class);


            /**
             * 当前页 小于 总页数 hasMore 为true，等于为 false
             *
             * 思路
             * 1，聚合查询出当前houseid的总条数 ： total
             *
             * 2，计算出总页数
             *
             * totalPage  = total % size == 0 ? total / size : total / size + 1
             *
             * 3, 比较 page 和 totalPage 设置 hasMore
             *
             */




            return LivegoodsResult.ok(comments);
        } catch (Exception e) {
            e.printStackTrace();
            return LivegoodsResult.error("查询评论失败，请联系管理员...");
        }
    }
}
